CPP problem A-B

大数相减

Give you two numbers A and B.

You job is to calculator the result of A - B.

A, B is ranged from 0 ~ 10^100. (guarantee that A >= B)


Sample Input

3 2

1000000000000001 1

Sample Output

1

1000000000000000

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
using namespace std;

void process(string&a, string&b)
{
int p[200] = { 0 };
int q[200] = { 0 };
int cura = 0,curb = 0;

for (int i = a.length() - 1; i >= 0; i--)//复制a数组
{
p[cura] = a[i]-'0';
cura++;
}

for (int i = b.length() - 1; i >= 0; i--)//复制b数组
{
q[curb] = b[i]-'0';
curb++;
}

int cur = 0;

while (cur < 199)//从末尾开始相减,并处理特殊情况
{
p[cur] -= q[cur];
if (p[cur] < 0)
{
p[cur + 1]--;
p[cur] += 10;
}
cur++;
}

for ( ; cur >= 0; cur--)//从头查找第一个不是0的数字
if (p[cur] != 0) break;

for ( ; cur >= 0; cur--)//从第一个不是0的数字开始打印
cout << p[cur];

cout << endl;
}

int main()
{
string a, b;

while (cin >> a >> b)
{
process(a, b);
}

return 0;
}

cpp文件在这里